<template>
  <div>
    <el-form :model="form" ref="userAdd" :rules="rules">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="user">
        <el-input v-model="form.user"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="form.state" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="addUser">立即添加</el-button>
      <el-button @click="closeAdd()">取消添加</el-button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    closeAdd: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '',
        email: '',
        phone: '',
        user: '',
        state: 1
      },
      // 表单验证规则
      rules: {
        name: [
          {
            required: true,
            // 验证不通过时，错误提示文案
            message: '当前字段是必填的'
          }
        ],
        email: [
          {
            required: true,
            // 验证不通过时，错误提示文案
            message: '当前字段是必填的'
          }
        ],
        phone: [
          {
            required: true,
            // 验证不通过时，错误提示文案
            message: '当前字段是必填的'
          }
        ],
        user: [
          {
            required: true,
            // 验证不通过时，错误提示文案
            message: '当前字段是必填的'
          }
        ]
      }
    }
  },
  methods: {
    addUser() {
      this.$refs.userAdd.validate().then(() => {
        this.$emit('addUser', this.form)
      }).catch(() => {
        this.$message({
          type: 'error',
          message: '输入有误，请重新输入'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>